package presenter;

import com.zhon.baselib.mvpbase.baseImpl.BasePresenterImpl;
import com.zhon.baselib.retroft.ExceptionHelper;

import java.util.List;
import java.util.Map;

import api.MainService;
import bean.TestBean;
import contact.TestContact;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


public class MainPresenter extends BasePresenterImpl<TestContact.view> implements TestContact.presenter {

    private Map<String,String> map;
    public MainPresenter(TestContact.view view,Map<String,String> map) {
        super(view);
        this.map = map;
    }

    @Override
    public void getData() {
        MainService.getInstance().commonService(map)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        addDisposable(disposable);
                        view.showLoadingDialog("");
                    }
                })
                .map(new Function<TestBean, List<TestBean.ResultBean.DataBean>>() {
                    @Override
                    public List<TestBean.ResultBean.DataBean> apply(@NonNull TestBean testBean) throws Exception {
                        return testBean.getResult().getData();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<TestBean.ResultBean.DataBean>>() {
                    @Override
                    public void accept(@NonNull List<TestBean.ResultBean.DataBean> storiesBeen) throws Exception {
                        view.dismissLoadingDialog();
                        view.setData(storiesBeen);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        view.dismissLoadingDialog();
                        ExceptionHelper.handleException(throwable);
                    }
                });
    }
}
